package response

import (
	"net/http"
	"vblog/exception"

	"github.com/gin-gonic/gin"
)

// 返回gin c.JSON
func Success(c *gin.Context, data any) {
	c.JSON(http.StatusOK, data)

}

func Failed(c *gin.Context, err error) {
	// 错误是ApiException 或其它类型时
	httpCode := http.StatusInternalServerError
	// httpCode := http.StatusOK
	if v, ok := err.(*exception.ApiException); ok {
		if v.HttpCode != 0 {
			httpCode = v.HttpCode
		}
	} else {
		//
		err = exception.ErrorServerInternal(err.Error())
	}

	c.JSON(httpCode, err)
	c.Abort()
}
